package com.bangcommunity.bbframe.sdm.validation;

import com.bangcommunity.bbframe.common.utils.lang.EnumUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Created by tanghc on 17/1/25.
 */
public class IsEnumValidator implements ConstraintValidator<IsEnum, String> {

    private Class<? extends Enum> clazz;

    @Override
    public void initialize(IsEnum val) {
        this.clazz = val.clazz();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        // null values are valid
        if (value == null) {
            return true;
        }
        return EnumUtils.getEnum(clazz, value) != null;
    }
}